package com.naiterui.ehp.bs.im.utils;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import java.util.concurrent.TimeUnit;

/**
 * 异步执行Util
 */
public class AsynUtils {

    // 线程池维护线程的最少数量
    private static final int COREPOOLSIZE = 5;
    // 线程池维护线程的最大数量
    private static final int MAXINUMPOOLSIZE = 20;
    // 线程池维护线程所允许的空闲时间
    private static final long KEEPALIVETIME = 5;
    // 线程池维护线程所允许的空闲时间的单位
    private static final TimeUnit UNIT = TimeUnit.MINUTES;
    // 线程池所使用的缓冲队列
    private static final BlockingQueue<Runnable> WORKQUEUE = new ArrayBlockingQueue<Runnable>(100);
    // 线程池对拒绝任务的处理策略：
    /*
     * AbortPolicy抛出异常
     * CallerRunsPolicy重试添加当前的任务，自动重复调用execute()方法
     * DiscardOldestPolicy抛弃旧的任务
     * DiscardPolicy抛弃当前的任务
     */
    private static final CallerRunsPolicy HANDLER = new ThreadPoolExecutor.CallerRunsPolicy();

    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(COREPOOLSIZE, MAXINUMPOOLSIZE, KEEPALIVETIME, UNIT, WORKQUEUE, HANDLER);

    /**
     * AsynExecutor.amrToMp3()
     *
     * @param filePath
     * @param newPath
     *
     * @Author gaoll
     * @Date 2016年11月3日
     * @since 2.6.0
     */
    public static void amrToMp3(final String filePath, final String newPath) {

        executor.execute(() -> MediaUtils.amrToMp3(filePath, newPath));
    }

}
